package com.kk.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.kk.dao.mybatis.mapper", sqlSessionTemplateRef = "mybatisSqlSessionTemplate")
@EnableTransactionManagement
public class MybatisDataSourceConfig {

    static final String MAPPER_LOCATION = "classpath:mapper/*.xml";

    @Autowired
    @Qualifier("mybatisDataSource")
    private DataSource mybatisDataSource;

    @Primary
    @Bean(name = "mybatisTransactionManager")
    public DataSourceTransactionManager mybatisTransactionManager() {
        return new DataSourceTransactionManager(mybatisDataSource);
    }

    /**
     * 需注意：
     * 1.此处需注意返回类必须是SqlSessionFactory,不能是MybatisSqlSessionFactoryBean
     * 2.如果是用Mybatis框架，把MybatisSqlSessionFactoryBean修改成SqlSessionFactoryBean就行
     */
    @Primary
    @Bean(name = "mybatisSqlSessionFactory")
    public SqlSessionFactory mybatisSqlSessionFactory() throws Exception {
        //mybatis
        //SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        //mybatis-plus
        final MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(mybatisDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MybatisDataSourceConfig.MAPPER_LOCATION));
        MybatisConfiguration configuration = new MybatisConfiguration();
        //configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        //  sqlSessionFactory.setPlugins(new Interceptor[]{ //PerformanceInterceptor(),OptimisticLockerInterceptor()
        //          paginationInterceptor() //添加分页功能
        //  });
        //sqlSessionFactory.setGlobalConfig(globalConfiguration());
        bean.setConfiguration(configuration);
        return bean.getObject();
    }

    @Bean(name = "mybatisSqlSessionTemplate")
    public SqlSessionTemplate mybatisSqlSessionTemplate(@Qualifier("mybatisSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
